﻿using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using Flurl;
using Newtonsoft.Json;
using ZilLion.Core.Unities.Config;
using ZilLion.Core.Unities.UnitiesMethods;
using ZilLion.Core.Unities.UnitiesMethods.WebApi.Http;
using ZilLion.TPO2O.SDK.EntityBase;

namespace ZilLion.TPO2O.SDK.Request
{
    public class DefaultO2ORequester : IO2ORequester
    {
        /// <summary>
        ///     目标平台
        /// </summary>
        private readonly TpType _targetTpType;

        public DefaultO2ORequester(TpType targetTpType)
        {
            _targetTpType = targetTpType;
        }

        /// <summary>
        ///     业务请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="requestBusinessKey"></param>
        /// <param name="urlParamdic"></param>
        /// <param name="requestBody"></param>
        /// <param name="requestType"></param>
        /// <returns></returns>
        public virtual O2OResponseResultInfo BusinessRequest<T>(string requestBusinessKey,
            IDictionary<string, object> urlParamdic = null,
            T requestBody = null, RequestType requestType = RequestType.Default) where T : class
        {
            var requestBodyString = string.Empty;
            if (requestBody != null)
                requestBodyString = JsonConvert.SerializeObject(requestBody);
            return BusinessRequest(requestBusinessKey, urlParamdic, requestBodyString, requestType);
        }

        /// <summary>
        ///     业务请求
        /// </summary>
        /// <param name="requestBusinessKey"></param>
        /// <param name="urlParamdic"></param>
        /// <param name="requestBodystring"></param>
        /// <param name="requestType"></param>
        /// <returns></returns>
        public virtual O2OResponseResultInfo BusinessRequest(string requestBusinessKey,
            IDictionary<string, object> urlParamdic = null,
            string requestBodystring = null, RequestType requestType = RequestType.Default)
        {
            O2OResponseResultInfo result = null;

            #region 处理url

            var requesturl = Url.Combine(CreateRequestUrl(), requestBusinessKey);
            var token = GetToken();
      
            requesturl = requesturl + $"?token={token}";
            if (urlParamdic != null)
            {
                var urlparam = JsonConvert.SerializeObject(urlParamdic).EncodeBase64();
                requesturl = requesturl + $"&urlparam={urlparam}";
            }

            #endregion

            if (requestType == RequestType.Default)
                if (requestBodystring.IsNullOrEmpty()) //如果没有有请求body为Get
                    result = HttpHelper.HttpRequestGet<O2OResponseResultInfo>(requesturl);
                else
                    result = HttpHelper.HttpRequestPost<O2OResponseResultInfo>(requesturl,
                        new StringContent(requestBodystring));
            else
                switch (requestType)
                {
                    case RequestType.Get:
                        result = HttpHelper.HttpRequestGet<O2OResponseResultInfo>(requesturl);
                        break;
                    case RequestType.Post:
                        result = HttpHelper.HttpRequestPost<O2OResponseResultInfo>(requesturl,
                            new StringContent(requestBodystring));
                        break;
                    case RequestType.Put:
                        result = HttpHelper.HttpRequestPut<O2OResponseResultInfo>(requesturl,
                            new StringContent(requestBodystring));
                        break;
                    default:
                        break;
                }
            return result;
        }

        protected virtual string CreateRequestUrl()
        {
            var host = ZilLionConfigurationManager.GetInstance().GetAppSetting("o2OWepApiHost");
            var tpTypeName = _targetTpType.ToString();


            return Url.Combine(host, "Api", tpTypeName);
        }

        public virtual string GetToken()
        {
            return TokenContainer.Instance().TryGetToken();
        }
    }
}